﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Immune;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Immune
{
    /// <summary>
    /// 根据生产批次获取免疫信息请求命令处理类
    /// </summary>
    public class GetImmuneHandler:IRequestHandler<GetImmuneCommand,ApiResult<List<ImmuneDTO>>>
    {
        IBaseRepository<Immune_Information> immuneRep;
        IMapper mapper;

        public GetImmuneHandler(IBaseRepository<Immune_Information> immuneRep, IMapper mapper)
        {
            this.immuneRep = immuneRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<ImmuneDTO>>> Handle(GetImmuneCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<ImmuneDTO>> res = new ApiResult<List<ImmuneDTO>>();
                var Immunes = immuneRep.GetAll().Where(x => x.Production_Batch == request.production_Batch&&!x.IsDeleted);
                var model = mapper.Map<List<ImmuneDTO>>(Immunes);
                res.Msg = "根据生产批次获取免疫信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
